import { MineModel } from '../../../../models/mine.js'
const GoldModel = new MineModel()
const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    GoldInfoModel: {},
    GoldList: [
      {
        "id":"9e1bbf7f142049f696fb63b87fb972c2",
        "userId":"1e20ebcabe324a23aeab76934022f610",
        "recordDetail":"每日签到完成",
        "recordType":2,
        "dealMoney":4444,
        "dealType":2,
        "dealDate":1540536686000,
        "userType":2
      },
      {
        "id":"9e1bbf7f142049f696fb63b87fb972c2",
        "userId":"1e20ebcabe324a23aeab76934022f610",
        "recordDetail":"每日签到完成",
        "recordType":2,
        "dealMoney":4444,
        "dealType":2,
        "dealDate":1540536686000,
        "userType":2
      }
    ],
    pageIndex: 1,
    pageSize: 10,
    loading: false,
    hasNext: true,
    empty:false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false
  },

  //查看金币说明
  tapToGoldRules: function (event) {
    const ruleType = 2;
    wx.navigateTo({
      url: '/pages/rules/index?ruleType='+ruleType
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //查询用户账户的金币明细
    const self = this;
    const params = {
      pageIndex:this.data.pageIndex,
      pageSize:this.data.pageSize
    };

    GoldModel.getUserGold(params).then(res => {
      if (res.data.userGoldRecordList.length == 0) {
        self.data.empty = true
      } else {
        self.data.empty = false
      }
      this.setData({
        empty:self.data.empty,
        GoldInfoModel: res.data,
        hasNext: res.data.hasNext,
        loading: false
      })
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex:1, 
      pageSize: 10
    })
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },


  //加载更多
  loadMore(isPull) {
    const self = this;  
    if (self.data.loading) {
      return
    }
    if (self.data.hasNext) {
      const params = {
        pageIndex: self.data.pageIndex,
        pageSize: self.data.pageSize
      }
      if (isPull) {
        self.setData({
          loading: true
        })
      }
    
      GoldModel.getUserGold(params).then(res => {
        self.data.GoldInfoModel.userGoldRecordList = self.data.GoldInfoModel.userGoldRecordList.concat(res.data.userGoldRecordList)
        self.setData({
          GoldInfoModel: self.data.GoldInfoModel,
          hasNext: res.data.hasNext,
          loading: false
        })
        wx.stopPullDownRefresh();
      }).catch();
    }
  },
  
  //上滑到底
  lower: function () {
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },
})